using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeoFramework.IO;
using GeoFramework.Gps.Nmea;

namespace GeoFramework.Examples
{
    public partial class AsynchronousDetectingDevicesForm : Form
    {
        public AsynchronousDetectingDevicesForm()
        {
            InitializeComponent();

            /* The "Devices" class contains methods for detecting GPS devices and controlling
             * settings used by GPS data interpreters. 
             */

            // Become notified when device detection starts and stops
            Devices.DeviceDetectionAttempted += new DeviceEventHandler(Devices_DeviceDetectionAttempted);
            Devices.DeviceDetectionStarted += new EventHandler(Devices_DeviceDetectionStarted);
            Devices.DeviceDetectionCompleted += new EventHandler(Devices_DeviceDetectionCompleted);

            // Become notified when a new device is detected
            Devices.DeviceDetected += new DeviceEventHandler(Devices_DeviceDetected);

            // Become notified when an exception is thrown
            Devices.DeviceDetectionExceptionOccurred += new ExceptionEventHandler(Devices_ExceptionOccurred);

            // Has a GPS device already been discovered?
            if (Devices.IsDeviceDetected)
            {
                // Yes.  Set the property grid to it
                DevicePropertyGrid.SelectedObject = Devices.GetDeviceByProtocol(NmeaInterpreter.ProtocolName);
            }
            else
            {
                // No.  Start device detection now
                Devices.Detect();

                /* NOTE: If device detection is already in progress, calling this
                 * method again has no effect.
                 */
            }

            // Is detection in progress?
            if (Devices.IsDetectionInProgress)
            {
                // Yes.  Show that we're waiting for a device
                StatusBar.Text = "Detection is in progress, please wait...";
            }
        }


        #region When a GPS Device is Detected

        private void Devices_DeviceDetected(object sender, DeviceEventArgs e)
        {
            // And use Invoke to update the form
            DevicePropertyGrid.SelectedObject = e.Device;

            // Also update the status bar
            StatusBar.Text = "Found device: " + e.Device.ToString();
        }

        #endregion

        #region When an Exception is Thrown During Device Detection

        private void Devices_ExceptionOccurred(object sender, ExceptionEventArgs e)
        {
            // Raise the exception
            StatusBar.Text = "An exception was encountered during detection.";
            MessageBox.Show(e.Exception.ToString());
        }

        #endregion

        #region When Device Detection is Started & Stopped

        private void Devices_DeviceDetectionStarted(object sender, EventArgs e)
        {
            StatusBar.Text = "Starting device detection...";
        }

        private void Devices_DeviceDetectionAttempted(object sender, DeviceEventArgs e)
        {
            // And notify of the detection attempt
            StatusBar.Text = "Attempting detection on " + e.Device.ToString();
        }

        private void Devices_DeviceDetectionCompleted(object sender, EventArgs e)
        {
            StatusBar.Text = "Device detection is complete.";
        }

        #endregion
    }
}